// #packager-optimize

(function() {

    var i18n = {

        messagesBundles : { },
        
        registerMessagesBundle : function(bundleName, bundle) {
            this.messagesBundles[bundleName] = bundle;
            logger.debug('Messages bundle %s registered.', bundleName);
        },

        getMessage : function(message) {
            if (typeof message != 'object') return message;
            return message;
        },

        findMessage : function(bundleName, messageKey) {
            if (bundleName) return this.getMessage(this.messagesBundles[bundleName][messageKey]);

            var currentBundleName;

            for (currentBundleName in this.messagesBundles) {
                var message = this.messagesBundles[currentBundleName][messageKey];
                if (message) return this.getMessage(message);
            }

            return null;
        },

        get : function(messageKey, o) {
            var bundleName = null;

            if (messageKey.indexOf(':') != -1) {
                var keyParts = messageKey.split(':');
                bundleName = keyParts[0];
                messageKey = keyParts[1];
            }

            return this.findMessage(bundleName, messageKey);
        }

    };

    juliet.registerModule('juliet.i18n', 'i18n-0.1', i18n);

})();
